package dk.eclipse.android.helpers;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * This helper handles preferences.
 * 
 * @author Ronni
 *
 */
public class PreferenceHelper {

	/**
	 * Returns a String value from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param defaultValue
	 * @return	the value for a given property value for a preference name.
	 */
	public static String getPreferenceString(Context context,
			String preferenceName, String propertyName, String defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getString(propertyName, defaultValue);
	}

	/**
	 * Returns a boolean value from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param defaultValue
	 * @return	the value for a given property value for a preference name.
	 */
	public static boolean getPreferenceBoolean(Context context,
			String preferenceName, String propertyName, boolean defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getBoolean(propertyName, defaultValue);
	}

	/**
	 * Returns a float value from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param defaultValue
	 * @return	the value for a given property value for a preference name.
	 */
	public static float getPreferenceFloat(Context context,
			String preferenceName, String propertyName, float defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getFloat(propertyName, defaultValue);
	}

	/**
	 * Returns a long value from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param defaultValue
	 * @return	the value for a given property value for a preference name.
	 */
	public static long getPreferenceLong(Context context,
			String preferenceName, String propertyName, long defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getLong(propertyName, defaultValue);
	}

	/**
	 * Returns a int value from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param defaultValue
	 * @return	the value for a given property value for a preference name.
	 */
	public static int getPreferenceInt(Context context,
			String preferenceName, String propertyName, int defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getInt(propertyName, defaultValue);
	}

	/**
	 * Returns all values from a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @return	the value for a given property value for a preference name.
	 */
	public static Map<String, ?> getPreferenceAll(Context context,
			String preferenceName) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		return settings.getAll();
	}

	/**
	 * Sets a String value to a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 */
	public static void setPreferenceString(Context context,
			String preferenceName, String propertyName, String value) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		edit.putString(propertyName, value);
		edit.commit();
	}

	/**
	 * Sets a boolean value to a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 */
	public static void setPreferenceBoolean(Context context,
			String preferenceName, String propertyName, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		edit.putBoolean(propertyName, value);
		edit.commit();
	}

	/**
	 * Sets a long value to a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 */
	public static void setPreferenceLong(Context context,
			String preferenceName, String propertyName, long value) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		edit.putLong(propertyName, value);
		edit.commit();
	}

	/**
	 * Sets a float value to a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 */
	public static void setPreferenceFloat(Context context,
			String preferenceName, String propertyName, float value) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		edit.putFloat(propertyName, value);
		edit.commit();
	}

	/**
	 * Sets a int value to a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 */
	public static void setPreferenceInt(Context context,
			String preferenceName, String propertyName, int value) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		edit.putInt(propertyName, value);
		edit.commit();
	}

	/**
	 * Returns the editor for a named preference store.
	 * 
	 * @param context
	 * @param preferenceName
	 * @param propertyName
	 * @param value
	 * @return 
	 */
	public static Editor getPreferenceEditor(Context context,
			String preferenceName, String propertyName) {
		SharedPreferences settings = context.getSharedPreferences(
				preferenceName, 0);
		Editor edit = settings.edit();
		return edit;
	}

}
